package com.app.housemanage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.app.housemanage.domain.Owners;
import com.app.housemanage.domain.Properties;
import com.app.housemanage.domain.PropertyTypes;
import com.app.housemanage.domain.dto.PropertyQueryDTO;
import com.app.housemanage.domain.vo.PropertiyVo;
import com.app.housemanage.mapper.OwnersMapper;
import com.app.housemanage.mapper.PropertiesMapper;
import com.app.housemanage.mapper.PropertyTypesMapper;
import com.app.housemanage.service.PropertiesService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 房产信息服务实现类
 */
@Service
public class PropertiesServiceImpl extends ServiceImpl<PropertiesMapper, Properties>
        implements PropertiesService {

    @Autowired
    private PropertyTypesMapper propertyTypesMapper;

    @Autowired
    private OwnersMapper ownersMapper;

    /**
     * 根据楼栋号查询房产列表
     */
    @Override
    public List<String> getPropertiesByBuilding() {
        List<Properties> list = this.list();
        List<String> buildingList = list.stream().map(Properties::getBuilding).distinct().toList();
        return buildingList;
    }

    /**
     * 根据房产状态查询房产列表
     */
    @Override
    public List<Properties> getPropertiesByStatus(String propertyStatus) {
        QueryWrapper<Properties> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("property_status", propertyStatus);
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 根据业主ID查询房产列表
     */
    @Override
    public List<Properties> getPropertiesByOwnerId(Integer ownerId) {
        QueryWrapper<Properties> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("owner_id", ownerId);
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 根据多个条件查询房产列表
     */
    @Override
    public List<PropertiyVo> searchProperties(PropertyQueryDTO propertyQueryDTO) {
        IPage page = new Page<>(propertyQueryDTO.getPageNum(), propertyQueryDTO.getPageSize());
        LambdaQueryWrapper<Properties> propertiesLambdaQueryWrapper = new LambdaQueryWrapper<>();
        propertiesLambdaQueryWrapper.like(ObjectUtil.isNotEmpty(propertyQueryDTO.getBuilding()),Properties::getBuilding, propertyQueryDTO.getBuilding())
                .eq(ObjectUtil.isNotEmpty(propertyQueryDTO.getPropertyCode()),Properties::getPropertyCode, propertyQueryDTO.getPropertyCode())
                .eq(ObjectUtil.isNotEmpty(propertyQueryDTO.getPropertyStatus()),Properties::getPropertyStatus, propertyQueryDTO.getPropertyStatus());
        IPage page1 = this.page(page, propertiesLambdaQueryWrapper);
        List<Properties> records = page1.getRecords();
        List<PropertiyVo> list = records.stream().map(properties -> {
            Integer typeId = (Integer) properties.getTypeId();
            Integer ownerId = properties.getOwnerId();
            Owners owners = ownersMapper.selectById(ownerId);
            PropertyTypes propertyTypes = propertyTypesMapper.selectById(typeId);
            PropertiyVo bean = BeanUtil.toBean(properties, PropertiyVo.class);
            bean.setTypeId(propertyTypes.getTypeName());
            bean.setOwner(owners.getOwnerName());
            bean.setStatus(properties.getPropertyStatus());
            return bean;
        }).toList();
        return list;
    }

    /**
     * 获取所有房产列表
     */
    @Override
    public List<Properties> getAllProperties() {
        return baseMapper.selectList(null);
    }

    /**
     * 分页查询所有房产
     */
    @Override
    public Page<Properties> getPropertiesPage(Integer pageNum, Integer pageSize) {
        Page<Properties> page = new Page<>(pageNum, pageSize);
        return baseMapper.selectPage(page, null);
    }

    /**
     * 分页查询房产（带条件）
     */
    @Override
    public Page<Properties> searchPropertiesPage(Integer pageNum, Integer pageSize,
                                                 String building, String room, String propertyStatus) {
        Page<Properties> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Properties> queryWrapper = new QueryWrapper<>();

        if (building != null && !building.isEmpty()) {
            queryWrapper.eq("building", building);
        }

        if (room != null && !room.isEmpty()) {
            queryWrapper.eq("room", room);
        }

        if (propertyStatus != null) {
            queryWrapper.eq("property_status", propertyStatus);
        }

        return baseMapper.selectPage(page, queryWrapper);
    }

    /**
     * 根据楼栋号分页查询房产
     */
    @Override
    public Page<Properties> getPropertiesByBuildingPage(Integer pageNum, Integer pageSize, String building) {
        Page<Properties> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Properties> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("building", building);
        return baseMapper.selectPage(page, queryWrapper);
    }

    /**
     * 根据状态分页查询房产
     */
    @Override
    public Page<Properties> getPropertiesByStatusPage(Integer pageNum, Integer pageSize, String propertyStatus) {
        Page<Properties> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Properties> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("property_status", propertyStatus);
        return baseMapper.selectPage(page, queryWrapper);
    }
}




